package de.mischa.nichtraucherDeluxe;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.util.Log;
import android.widget.RemoteViews;

public class RoundNonSmokerWidget extends AppWidgetProvider{

	public static String OPEN_CONFIGVIEW = "OpenConfigView";
	private DbAdapter dbAdapter;
	private Intent intent;
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i("onUpdate", "Start");
		for (int appWidgetId : appWidgetIds) {

			intent = new Intent(context, NichtraucherDeluxeActivity.class);
			intent.setAction(OPEN_CONFIGVIEW);
			

			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
					intent, 0);

			RemoteViews view = new RemoteViews(context.getPackageName(),
					R.layout.roundnonsmokingwidget);
			view.setOnClickPendingIntent(R.id.widgetLayout, pendingIntent);
			load(view, context);

			appWidgetManager.updateAppWidget(appWidgetId, view);

		}

	}
	public void onEnabled(Context context) {
		Log.v("onEnabled", "called onEnabled");
		AppWidgetManager mgr = AppWidgetManager.getInstance(context);


		// Get the layout for the App Widget
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.roundnonsmokingwidget);

		// attach the click listener for the service start command intent
		// views.setOnClickPendingIntent(R.id.button, myPI);

		// define the componenet for self
		ComponentName comp = new ComponentName(context.getPackageName(),
				RoundNonSmokerWidget.class.getName());

		// tell the manager to update all instances of the toggle widget with
		// the click listener
		mgr.updateAppWidget(comp, views);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		Log.v("onReceive", "called onCreceive");
		if (intent.getAction().equals(OPEN_CONFIGVIEW)) {
			Log.v("onReceive", OPEN_CONFIGVIEW);
		}
	}
	private void load(RemoteViews view, Context context) {
		dbAdapter = new DbAdapter(context);
		dbAdapter.open();
		Cursor cursor = dbAdapter.fetchEntry();
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();

			Calendar cal_1 = new GregorianCalendar();
			Calendar cal_2 = new GregorianCalendar();
			cal_1.set(cursor.getInt(cursor.getColumnIndex("year")),
					cursor.getInt(cursor.getColumnIndex("month")),
					cursor.getInt(cursor.getColumnIndex("day")),
					cursor.getInt(cursor.getColumnIndex("hour")),
					cursor.getInt(cursor.getColumnIndex("minute")));

			long time = cal_2.getTimeInMillis() - cal_1.getTimeInMillis(); // Differenz
																			// in
																			// ms


			long days = time / (1000 * 60 * 60 * 24);
			time = time - (days * 24 * 60 * 60 * 1000);
			
			Resources res = context.getResources();
			
			view.setTextViewText(R.id.lblroundMain, days + res.getString(R.string.days));

			
		} else {
			view.setTextViewText(R.id.lblroundMain, "Click Me");
		}

		cursor.close();
		dbAdapter.close();
	}
}
